

#include "cled_ctrl.h"
#ifdef CLED_USE_POLSYS
#include "polsys.h"
#endif

// 定义一个任务，如果没有使用polsys可以不用定义，在别处定时调用LEDC_CtrlLoop任务入口函数即可
#ifdef CLED_USE_POLSYS
static pdata polsys_task_t mTask;
static bit mTaskIsRun = false;
#endif
static pdata int mLedNum = 0;
 pdata cled_t *mLedList[CLED_MAX_LED];

static void Run(cled_t *obj);

// 任务入口：10ms循环执行
// 如果不使用 Polsys 需要定时调用
void CLEDC_CtrlLoop()
{
    int i;
    for (i = 0; i < mLedNum; i++)
    {
        Run(mLedList[i]);
    }
}

// 注册(初始化)一个LED，让其受LEDC控制
void CLEDC_Regist(cled_t *obj, cled_set_value_func_t set_value_func)
{
    if (mLedNum < CLED_MAX_LED)
    {
        obj->setValue = set_value_func;
        mLedList[mLedNum] = obj;
        mLedNum++;
    }
#ifdef CLED_USE_POLSYS
    if (mTaskIsRun == false)
    {
        Polsys_TaskInit(&mTask, CLEDC_CtrlLoop, 10);
        mTaskIsRun = true;
    }
#endif
}

// 设置颜色 AARRGGBB
void CLEDC_SetColor(cled_t *obj, u32 color)
{
    obj->mColor.all = color;
}

// 设置LED动作
// action_value - 闪烁次数、时长（单位10ms）
void CLEDC_SetAction(cled_t *obj, cled_action_t action, int action_value)
{
    obj->mAction = action;
    obj->mActionValue = action_value;
}

// 获取当前在执行的动作
FORCE_IN_LINE cled_action_t CLEDC_GetAction(cled_t *obj)
{
    return obj->mAction;
}

// 设置实时亮度
static void SetBrightness(cled_t *obj, u8 brightness)
{
    color_t value;
    value.all = 0;
    value.r = (int)obj->mColor.r * brightness / 255;
    value.g = (int)obj->mColor.g * brightness / 255;
    value.b = (int)obj->mColor.b * brightness / 255;
    obj->setValue(value);
}

// 间隔10ms运行一次确保LED控制正常执行
static void Run(cled_t *obj)
{
    switch (obj->mAction)
    {
    case CLED_ON:
        if (obj->mActionValue > 0)
        {
            obj->mActionValue--;
            SetBrightness(obj, 255);
        }
        else
            obj->mAction = CLED_OFF;
        break;

    case CLED_OFF:
        SetBrightness(obj, 0);
        obj->mAction = CLED_NULL;
        break;

    case CLED_FLASH:
        if (obj->mTime < 20)
            SetBrightness(obj, 0);
        else if (obj->mTime < 40)
            SetBrightness(obj, 255);
        else
        {
            obj->mTime = 0;
            obj->mActionValue--;
            if (obj->mActionValue == 0)
                obj->mAction = CLED_OFF;
        }
        obj->mTime++;
        break;
    
    case CLED_GRA_ON:
			if(obj->mActionValue>0) // 亮的时间
			{
				obj->mActionValue--;
				if(obj->mCount<250)obj->mCount+=5;
				SetBrightness(obj, obj->mCount);
			}
			else {obj->mAction=CLED_GRA_OFF;}
			break;
    case CLED_GRA_OFF:
        if(obj->mCount>=5)obj->mCount-=5;
        else 
        {
            obj->mAction=CLED_NULL;
            obj->mCount=0;
        }
        SetBrightness(obj, obj->mCount);
        break;

    case CLED_GRA_BREATH:
        if(obj->mActionValue>0)
        {
            obj->mActionValue--;
            if(obj->mTime)
            {
                if(obj->mCount<=250)obj->mCount+=5;
                else obj->mTime=0;
            }
            else
            {
                if(obj->mCount>=5)obj->mCount-=5;
                else obj->mTime=1;
            }
            SetBrightness(obj, obj->mCount);
        }
        else {obj->mAction=CLED_GRA_OFF;}
        break;
    case CLED_GRA_COLOR:
        if(obj->mActionValue>0)
        {
            obj->mActionValue--;
            if(obj->mCount==0)	//R++
            {
                obj->mColor.g = 0;
                obj->mColor.b = 0;
                if(obj->mColor.r<214)obj->mColor.r++;
                else obj->mCount=1;
            }
            else if(obj->mCount==1)//步骤1：G++
            {
                if(obj->mColor.g<214)obj->mColor.g++;
                else obj->mCount=2;
            }
            else if(obj->mCount==2)//步骤2：R--
            {
                if(obj->mColor.r>0)obj->mColor.r--;
                else obj->mCount=3;
            }
            else if(obj->mCount==3)//步骤3：B++
            {
                if(obj->mColor.b<214)obj->mColor.b++;
                else obj->mCount=4;
            }
            else if(obj->mCount==4)//步骤4：G--
            {
                if(obj->mColor.g>0)obj->mColor.g--;
                else obj->mCount=5;			
            }
            else if(obj->mCount==5)//步骤5：R++
            {
                if(obj->mColor.r<214)obj->mColor.r++;
                else obj->mCount=6;	
            }
            else 				//步骤6：B--
            {
                if(obj->mColor.b>0)obj->mColor.b--;
                else obj->mCount=1;			
            }
            obj->setValue(obj->mColor);
		}
        else {obj->mAction=CLED_GRA_OFF;}
        break;

    default:
        obj->mAction = CLED_NULL;
        obj->mCount = 0;
        obj->mTime = 0;
        break;
    }
}



